× Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 Lesson 6 Lesson 7 Lesson 8 Lesson 9 Lesson 10 Lesson 11 Lesson 12 Lesson 13 Lesson 14 Lesson 15 Lesson 16 Lesson 17 Lesson 18 Lesson 19 Lesson 20 Lesson 21 Lesson 22 Lesson 23 Lesson 24 Lesson 25 Mini Lesson 1 Mini Lesson 2 Mini Lesson 3 Mini Lesson 4 Mini Lesson 5

Lessons

Lesson 16 - Input()

The input method can be very useful when a program you create needs information from the user. For instance you may need their name and age, or if it is a game, their username. Here's is the syntax for asking for someone's name:

name = input('Please enter your name: ')
print(name)

Let's say that I inputted into the console 'John Smith', without the quotation marks, the code would output 'John Smith', with quotation marks.

If we wanted to be a little more specific about their name, such as that we want to split their name up into first and last name, we could do this:

first_name, last_name = input().split()

We utilize the .split() function that we learned about early in the course, which was utilized to split up the string 'John Smith' so that first_name variable would be equal to 'John' and the last_name variable would be equal to 'Smith'.